package servlet;

import bean.Result;
import com.google.gson.Gson;
import dao.CartDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "SaveCardServlet", value = "/SaveCardServlet.s")
public class SaveCardServlet extends HttpServlet {

    private CartDao dao = new CartDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final String uid = request.getParameter("uid");
        final String proid = request.getParameter("proid");
        final String paymenttype = request.getParameter("paymenttype"); // 支付方式
        final String price = request.getParameter("price"); // 单价
        final String num = request.getParameter("num");
        int totalprice = Integer.parseInt(num) * Integer.parseInt(price);
        final String shoppingid = request.getParameter("shoppingid");
        String status = request.getParameter("status");

        Result result = null;

        try {
            java.sql.Timestamp time = new java.sql.Timestamp(new java.util.Date().getTime());//new java.util.Date().getTime()
            System.out.println(time);

            final String times = time.toString().substring(0,19);
            String paymenttime = time.toString().substring(0,19);
            int order = dao.saveOrder(uid, paymenttype,status, paymenttime,times, String.valueOf(totalprice),shoppingid); // 详细订单
            System.out.println(order);

            dao.saveOrderdetail(String.valueOf(order), proid, String.valueOf(totalprice), num);
            result = new Result(1, "购买成功！", null);
        } catch (Exception e){
            e.printStackTrace();
            result = new Result(0,e.getMessage(), null);
        }

        final String json = new Gson().toJson(result);
        response.getWriter().append(json);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
